package com.itcncp.ucenter.web.controller;

import com.itcncp.common.base.BaseController;
import com.itcncp.common.model.OrderCodeType;
import com.itcncp.common.rongCloud.models.SdkHttpResult;
import com.itcncp.common.rongCloud.models.SdkHttpResultToken;
import com.itcncp.common.util.*;
import com.itcncp.ucenter.common.ApiResult;
import com.itcncp.ucenter.common.ApiResultConstant;
import com.itcncp.ucenter.dao.model.*;
import com.itcncp.ucenter.rpc.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * Created by shuitcncp on 2016/12/10.
 */
@Controller
@RequestMapping("/user")
@Api(tags = "user",value = "用户信息", description = "用户的基本信息接口",
        produces = "produces",consumes = "consumes",basePath = "/user")

public class UserController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserCustomerInfoService userCustomerInfoService;
    @Autowired
    private UserAccountInfoService userAccountInfoService;
    @Autowired
    private UserVerifyJnlService userVerifyJnlService;
    @Autowired
    private ItcsFeedbackJnlService itcsFeedbackJnlService;
    @Autowired
    private ItcsNoticeJnlService itcsNoticeJnlService;
    @Autowired
    private ItcsCityInfoService itcsCityInfoService;
    @Autowired
    private ItcsCityLimitInfoService itcsCityLimitInfoService;



    //    @ApiResponses(value = {
//            @ApiResponse( code=200,message="成功",
//                    responseHeaders = { @ResponseHeader(name = "用户信息",description = "返回的用户具体信息列表",response = UserInfo.class)})
//    })
    @ApiOperation(value = "手机号与密码进行登录", response = UserCustomerInfo.class)
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object login(HttpServletRequest request,
                        @RequestParam(required = true,defaultValue = "",value = "mobile") String mobile,
                        @RequestParam(required = true,defaultValue = "",value = "password") String password,
                        @RequestParam(required = true,defaultValue = "",value = "deviceAppNo") String deviceAppNo,
                        @RequestParam(required = false,defaultValue = "0",value = "longitude") Double longitude,
                        @RequestParam(required = false,defaultValue = "0",value = "latitude") Double latitude
                        ) {

//        Boolean loginstatus =  new Boolean(request.getAttribute("loginstatus").toString());

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_ACCOUNT_PASS_ERROR);
        }

        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_ACCOUNT_PASS_ERROR);
        }

        //验证密码是否为空
        if(StringUtils.isEmpty( password)){
            return new ApiResult( ApiResultConstant.APP_ACCOUNT_PASS_ERROR);
        }

        //验证设备号是否为空
        if(StringUtils.isEmpty( deviceAppNo)){
            return new ApiResult( ApiResultConstant.APP_DEVICE_NO_UNKNOWN);
        }
        UserCustomerInfo userInfo = new UserCustomerInfo();
        userInfo.setMobile( mobile );
        userInfo.setPassword( password );
        userInfo.setDeviceAppNo( deviceAppNo );
        userInfo.setLoginMode(new Byte("1"));
        if(latitude>0){
            userInfo.setLatitude( latitude );
            userInfo.setLongitude( longitude );
        }
        userInfo = userCustomerInfoService.login( userInfo );

        if(null != userInfo){
            return  new ApiResult( ApiResultConstant.SUCCESS,userInfo );
        }

        return new ApiResult( ApiResultConstant.APP_ACCOUNT_PASS_ERROR);
    }


    @ApiOperation(value = "退出登录", response = UserCustomerInfo.class)
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public Object logout(HttpServletRequest request,
                         @RequestParam(required = true,defaultValue = "",value = "userId") Long userId) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        UserCustomerInfo userInfo = new UserCustomerInfo();
        userInfo.setUserId(userId);
        userInfo.setDeviceAppNo("");
        Integer result = userCustomerInfoService.updateByPrimaryKeySelective(userInfo);

        if(result > 0){
            return  new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }


    @ResponseBody
    @ApiResponse(code=200,message = "用户列表",response = UserCustomerInfo.class)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UserCustomerInfoExample userInfoExample = new UserCustomerInfoExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            userInfoExample.setOrderByClause(sort + " " + order);
        }
        if (StringUtils.isNotBlank(search)) {
            userInfoExample.or()
                    .andMobileLike("%" + search + "%");
        }
        List<UserCustomerInfo> rows = userCustomerInfoService.selectByExampleForStartPage(userInfoExample, offset, limit);
        Integer total = userCustomerInfoService.countByExample(userInfoExample);
        return new ApiResult( ApiResultConstant.SUCCESS,rows,total);
    }


    @ResponseBody
    @ApiResponse(code=200,message = "用户信息",response = UserCustomerInfo.class)
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public Object list(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        UserCustomerInfo userInfo = userCustomerInfoService.selectByPrimaryKey( userId );

        return new ApiResult( ApiResultConstant.SUCCESS,userInfo);
    }

    @ResponseBody
    @ApiResponse(code=200,message = "更新用户头像",response = UserCustomerInfo.class)
    @RequestMapping(value = "/updateHead", method = RequestMethod.POST)
    public Object updateHead(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
            @RequestParam(required = true,defaultValue = "",value = "head") String head) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }


        //验证用户昵称
        if(StringUtils.isEmpty( head )){
            return new ApiResult( ApiResultConstant.APP_NOT_NAME);
        }

        UserCustomerInfo userInfo  = new UserCustomerInfo();
        userInfo.setUserId( userId );
        userInfo.setHead( head );
        userInfo.setUpdateTime( new Date(  ) );

        Integer result = userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(result>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }


    @ResponseBody
    @ApiResponse(code=200,message = "更新用户昵称",response = UserCustomerInfo.class)
    @RequestMapping(value = "/updateNickname", method = RequestMethod.POST)
    public Object updateNickname(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
            @RequestParam(required = true,defaultValue = "",value = "nickname") String nickname) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }


        //验证用户昵称
        if(StringUtils.isEmpty( nickname )){
            return new ApiResult( ApiResultConstant.APP_NOT_NAME);
        }

        UserCustomerInfo userInfo  = new UserCustomerInfo();
        userInfo.setUserId( userId );
        userInfo.setNickname( nickname );
        userInfo.setUpdateTime( new Date(  ) );

        Integer result = userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(result>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

    @ResponseBody
    @ApiResponse(code=200,message = "更新用户地址",response = UserCustomerInfo.class)
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public Object updateAddress(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
            @RequestParam(required = true,defaultValue = "",value = "address") String address) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }


        //验证用户昵称
        if(StringUtils.isEmpty( address )){
            return new ApiResult( ApiResultConstant.APP_PARAM_ERROR);
        }

        UserCustomerInfo userInfo  = new UserCustomerInfo();
        userInfo.setUserId( userId );
        userInfo.setAgentAddress( address );
        userInfo.setUpdateTime( new Date(  ) );

        Integer result = userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(result>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

    @ResponseBody
    @ApiResponse(code=200,message = "更新支付方式",response = UserCustomerInfo.class)
    @RequestMapping(value = "/updatePayMethod", method = RequestMethod.POST)
    public Object payMethod(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
            @RequestParam(required = true,defaultValue = "0",value = "payMethod") Integer payMethod) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        //扣款方式 1非自动扣款 2自动扣款（不需要密码）
        if(payMethod==2){
            // 如果是将自动扣款状态改为非自动 判断用户金额是否 <=5元 不足返回错误提示
            UserAccountInfo userAccountInfo = userAccountInfoService.selectByPrimaryKey( userId );
            if(null != userAccountInfo){
                if(userAccountInfo.getAccountBalance().doubleValue() <= 500){
                    return new ApiResult( ApiResultConstant.APP_COMMODITY_NOT_AMOUNT);
                }
            }else{
                return new ApiResult( ApiResultConstant.FAILED);
            }
        }

        UserCustomerInfo userInfo  = new UserCustomerInfo();
        userInfo.setUserId( userId );
        userInfo.setPayMethod( payMethod.byteValue() );
        userInfo.setUpdateTime( new Date(  ) );

        Integer result = userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(result>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

    @ResponseBody
    @ApiResponse(code=200,message = "更新开票方式",response = UserCustomerInfo.class)
    @RequestMapping(value = "/updateInvoice", method = RequestMethod.POST)
    public Object updateInvoice(
            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
            @RequestParam(required = true,defaultValue = "0",value = "invoiceAutoFlag") Integer invoiceAutoFlag) {

        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        UserCustomerInfo userInfo  = new UserCustomerInfo();
        userInfo.setUserId( userId );
        userInfo.setInvoiceAutoFlag( invoiceAutoFlag.byteValue() );
        userInfo.setUpdateTime( new Date(  ) );

        Integer result = userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(result>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }


    @ApiOperation(value = "注册")
    @ResponseBody
    @ApiResponse(code=200,message = "注册成功",response = UserCustomerInfo.class)
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Object register(HttpServletRequest request,
                         @RequestParam(required = true,defaultValue = "",value = "mobile") String mobile,
                         @RequestParam(required = true,defaultValue = "",value = "password") String password,
                         @RequestParam(required = true,defaultValue = "",value = "deviceAppNo") String deviceAppNo,
                         @RequestParam(required = true,defaultValue = "",value = "security") String security,
                         @RequestParam(required = false,defaultValue = "0",value = "longitude") Double longitude,
                         @RequestParam(required = false,defaultValue = "0",value = "latitude") Double latitude ) {

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }
        //验证手机格式
        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        //验证密码是否为空
        if(StringUtils.isEmpty( password )){
            return new ApiResult( ApiResultConstant.APP_PARAM_ERROR);
        }

        //验证设备号是否为空
        if(StringUtils.isEmpty( deviceAppNo)){
            return new ApiResult( ApiResultConstant.APP_DEVICE_NO_UNKNOWN);
        }

        //验证安全码
        if(StringUtils.isEmpty( security )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR);
        }

        UserCustomerInfo userInfo =  new UserCustomerInfo();
        userInfo.setMobile( mobile );
        userInfo.setSecurity( security );
        userInfo.setDeviceAppNo( deviceAppNo );
        userInfo.setPassword( password );
        userInfo.setLoginMode(new Byte("1"));
        if(longitude>0) {
            userInfo.setLongitude( longitude );
            userInfo.setLatitude( latitude );
        }
        userInfo = userCustomerInfoService.registerInfo(userInfo);

        if(null != userInfo){
            return new ApiResult( ApiResultConstant.SUCCESS,userInfo);
        }

        return  new ApiResult( ApiResultConstant.APP_ACCOUNT_EXISTS );
    }

    /**
     * 重置密码
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "重置密码",response = ApiResultConstant.class)
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public  Object resetPwd(HttpServletRequest request,
                            @RequestParam(required = true,defaultValue = "0",value = "userId") Long userId,
                            @RequestParam(required = true,defaultValue = "",value = "mobile") String mobile,
                            @RequestParam(required = true,defaultValue = "",value = "password") String password,
                            @RequestParam(required = true,defaultValue = "",value = "deviceAppNo") String deviceAppNo,
                            @RequestParam(required = true,defaultValue = "",value = "security") String security,
                            @RequestParam(required = true,defaultValue = "",value = "registerCode") String registerCode){

        //验证设备号是否为空
        if(StringUtils.isEmpty( deviceAppNo )){
            return new ApiResult( ApiResultConstant.APP_DEVICE_NO_UNKNOWN);
        }
        //验证用户userId
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile(  mobile)){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        //密码是否为空
        if(StringUtils.isEmpty( password )){
            return new ApiResult( ApiResultConstant.APP_PASSWORD_TRIM);
        }

        //安全验证密码是否为空
        if(StringUtils.isEmpty( security )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR );
        }

        //验证密码是否为空
        if(StringUtils.isEmpty( registerCode )){
            return new ApiResult( ApiResultConstant.APP_SMS_ERROR );
        }

        UserCustomerInfo userInfo = userCustomerInfoService.selectByPrimaryKey(userId);

        if(null == userInfo){
            return  new ApiResult( ApiResultConstant.APP_ILLEGAL );
        }


        if(!userInfo.getMobile().equals( mobile )){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        //安全验证密码是否正确
        if(!userInfo.getSecurity().equals( AESUtil.encryptPassword( security ) )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR );
        }

        /**
         * 验证码类型
         * 1.用户注册验证码
         * 2.登录确认验证码
         * 3.修改密码验证码
         * 4.身份验证验证码
         * 5.信息变更验证码
         * 6.活动确认验证码
         */
        UserVerifyJnl userVerifyCode = userVerifyJnlService.verifyRegisterCode( mobile,registerCode,3);

        if(null == userVerifyCode){
            return new ApiResult(ApiResultConstant.APP_SMS_ERROR);
        }

        // 逻辑处理
        userInfo.setPassword(AESUtil.encryptPassword( password ));
        userInfo.setUserId( userId );

        /**
         * 先判断当前设备id与之前的设备id是否一致 一致则不需要重新获取 不一致则可能该用户在其他设备上进行登录，token重新获取
         * 保证其他设备不会收到通知
         */
        if (!userInfo.getDeviceAppNo().equals(deviceAppNo)) {
            // 登陆后注册融云
            SdkHttpResult resutl = rongCloud.getToken(userInfo.getDeviceAppNo(),
                    "", "");
            // 解析得到的数据
            SdkHttpResultToken token = JsonUtils.toBean(resutl.getResult(),
                    SdkHttpResultToken.class);
            // 更新用户当前登录的设备信息
            userInfo.setToken(token.getToken());
            userInfo.setDeviceAppNo(userInfo.getDeviceAppNo());
        }

        Integer total =  userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(total>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return  new ApiResult( ApiResultConstant.FAILED );

    }


    /**
     * 修改密码
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "修改密码",response = ApiResultConstant.class)
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    public  Object updatePwd(HttpServletRequest request,
                            @RequestParam(required = true,defaultValue = "",value = "mobile") String mobile,
                            @RequestParam(required = true,defaultValue = "",value = "password") String password,
                            @RequestParam(required = true,defaultValue = "",value = "deviceAppNo") String deviceAppNo,
                            @RequestParam(required = true,defaultValue = "",value = "security") String security,
                            @RequestParam(required = true,defaultValue = "",value = "registerCode") String registerCode){

        //验证设备号是否为空
        if(StringUtils.isEmpty( deviceAppNo )){
            return new ApiResult( ApiResultConstant.APP_DEVICE_NO_UNKNOWN);
        }

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile(  mobile)){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        //密码是否为空
        if(StringUtils.isEmpty( password )){
            return new ApiResult( ApiResultConstant.APP_PASSWORD_TRIM);
        }

        //安全验证密码是否为空
        if(StringUtils.isEmpty( security )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR );
        }

        //验证密码是否为空
        if(StringUtils.isEmpty( registerCode )){
            return new ApiResult( ApiResultConstant.APP_SMS_ERROR );
        }

        UserCustomerInfoExample userCustomerInfoExample = new UserCustomerInfoExample();
        userCustomerInfoExample.createCriteria().andMobileEqualTo(mobile);
        UserCustomerInfo userInfo = userCustomerInfoService.selectFirstByExample(  userCustomerInfoExample );

        if(null == userInfo){
            return  new ApiResult( ApiResultConstant.APP_ACCOUNT_NOT_EXISTS );
        }

        if(userInfo.getStatus()!=1){
            return  new ApiResult( ApiResultConstant.APP_ACCOUNT_LOCK);
        }

        //安全验证密码是否正确
        if(!userInfo.getSecurity().equals( AESUtil.encryptPassword( security ) )){
            return new ApiResult( ApiResultConstant.APP_SECURITY_ERROR );
        }

        /**
         * 验证码类型
         * 1.用户注册验证码
         * 2.登录确认验证码
         * 3.修改密码验证码
         * 4.身份验证验证码
         * 5.信息变更验证码
         * 6.活动确认验证码
         */
        UserVerifyJnl userVerifyCode = userVerifyJnlService.verifyRegisterCode( mobile,registerCode,3);

        if(null == userVerifyCode){
            return new ApiResult(ApiResultConstant.APP_SMS_ERROR);
        }

        // 逻辑处理
        userInfo.setPassword(AESUtil.encryptPassword( password ));

        Integer total =  userCustomerInfoService.updateByPrimaryKeySelective( userInfo );

        if(total>0){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return  new ApiResult( ApiResultConstant.FAILED );

    }
    /**
     * 反馈
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "APP反馈",response = ApiResultConstant.class)
    @RequestMapping(value = "/addFeedback", method = RequestMethod.POST)
    public Object addFeedback(HttpServletRequest request,
                              @RequestParam(required = true,defaultValue = "0",value = "")Long userId,
                              @RequestParam(required = false,defaultValue = "0",value = "")Long parentFeedbackId,
                              @RequestParam(required = true,defaultValue = "",value = "")String content,
                              @RequestParam(required = false,defaultValue = "",value = "")String contact){

        if(userId <= 0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL );
        }

        ItcsFeedbackJnl feedback = new ItcsFeedbackJnl();
        feedback.setUserId( userId );
        feedback.setParentFeedbackId( parentFeedbackId );
        feedback.setChannel( new Byte( "0" ) );
        feedback.setContact( content );
        feedback.setType( new Byte( "0" ) );
        feedback.setContent( content );
        feedback.setCreateTime( new Date(  ) );
        feedback.setFeedbackId( OrderUtils.createOrderId( OrderCodeType.FEEDBACK_ID ) );

        Integer total = itcsFeedbackJnlService.insertSelective( feedback );

        if(total>0){
            return  new ApiResult( ApiResultConstant.SUCCESS );
        }

        return new ApiResult( ApiResultConstant.FAILED );
    }

    /**
     * 系统通知
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "系统通知",response = ItcsNoticeJnl.class)
    @RequestMapping(value = "/findByNotice", method = RequestMethod.POST)
    public Object findByNotice(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "0", value = "pageSize") int pageSize,
                               @RequestParam(required = false, defaultValue = "5", value = "pageIndex") int pageIndex,
                               @RequestParam(required = false, defaultValue = "", value = "search") String search,
                               @RequestParam(required = false, value = "sort") String sort,
                               @RequestParam(required = false, value = "order")String order){

        ItcsNoticeJnlExample noticeExample = new ItcsNoticeJnlExample();
        //模糊查询
        if(StringUtils.isNotEmpty( search )) {
            noticeExample.or().andTitleLike( "%"+search+"%" )
                    .andSummaryLike( "%"+search+"%" )
                    .andContentLike( "%"+search+"%" );
        }
        //排序
        if (StringUtils.isNotEmpty(sort) && StringUtils.isNotEmpty(order)) {
            noticeExample.setOrderByClause(sort + " " + order);
        }
        //分页查询
        List<ItcsNoticeJnl> notices = itcsNoticeJnlService.selectByExampleForStartPage(noticeExample, pageSize, pageIndex);


        return new ApiResult( ApiResultConstant.SUCCESS,notices );
    }


    @ResponseBody
    @ApiResponse(code=200,message = "获取通知信息",response = ItcsNoticeJnl.class)
    @RequestMapping(value = "/getByNotice", method = RequestMethod.POST)
    public Object getByNotice(HttpServletRequest request,
                         @RequestParam(required = false, defaultValue = "0", value = "noticeId") Long noticeId){

        ItcsNoticeJnl notice = itcsNoticeJnlService.selectByPrimaryKey( noticeId );

        //查询一条推送信息
        return new ApiResult( ApiResultConstant.SUCCESS, notice);
    }

    /**
     * 验证短信码
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "验证注册短信码",response = ApiResultConstant.class)
    @RequestMapping(value = "/verifyRegisterCode", method = RequestMethod.POST)
    public  Object verifyRegisterCode(
            HttpServletRequest request,
            @RequestParam(value = "mobile",defaultValue = "") String mobile,
            @RequestParam(value = "registerCode",defaultValue = "") String registerCode){

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        //验证密码是否为空
        if(StringUtils.isEmpty( registerCode )){
            return new ApiResult( ApiResultConstant.APP_SMS_ERROR);
        }

        /**
         * 验证码类型
         * 1.用户注册验证码
         * 2.登录确认验证码
         * 3.修改密码验证码
         * 4.身份验证验证码
         * 5.信息变更验证码
         * 6.活动确认验证码
         */
        UserVerifyJnl itemCode = userVerifyJnlService.verifyRegisterCode(mobile,registerCode,1);

        if(null != itemCode){
            return new ApiResult( ApiResultConstant.SUCCESS);
        }

        return  new ApiResult( ApiResultConstant.APP_SMS_EXPIRED );
    }


    /**
     *  发送短信验证码
     */
    @ResponseBody
    @ApiResponse(code=200,message = "发送注册短信验证码",response = ApiResultConstant.class)
    @RequestMapping(value = "/getRegisterCode", method = RequestMethod.POST)
    public Object getRegisterCode(
            HttpServletRequest request,
            @RequestParam(value = "mobile",defaultValue = "") String mobile){

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        UserVerifyJnl item = userVerifyJnlService.getRegisterCode( mobile );

        if(null != item){
            return  new ApiResult( ApiResultConstant.SUCCESS );
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

    /**
     *  发送短信验证码
     */
    @ResponseBody
    @ApiResponse(code=200,message = "发送密码修改短信验证码",response = ApiResultConstant.class)
    @RequestMapping(value = "/getPasswordCode", method = RequestMethod.POST)
    public Object getPasswordCode(
            HttpServletRequest request,
            @RequestParam(value = "mobile",defaultValue = "") String mobile){

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        UserVerifyJnl item = userVerifyJnlService.getPasswordCode(mobile);

        if(null != item){
            return  new ApiResult( ApiResultConstant.SUCCESS );
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }


    /**
     *  发送短信验证码
     */
    @ResponseBody
    @ApiResponse(code=200,message = "发送信息变更短信验证码",response = ApiResultConstant.class)
    @RequestMapping(value = "/getInfoUpdateCode", method = RequestMethod.POST)
    public Object getInfoUpdateCode(
            HttpServletRequest request,
            @RequestParam(value = "mobile",defaultValue = "") String mobile){

        //验证手机号的正确性
        if(StringUtils.isEmpty( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        if(!IDCardUtil.mobile( mobile )){
            return new ApiResult( ApiResultConstant.APP_MOBILE_ERROR);
        }

        UserVerifyJnl item = userVerifyJnlService.getInfoUpdateCode(mobile);

        if(null != item){
            return  new ApiResult( ApiResultConstant.SUCCESS );
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

    /**
     *  限行信息
     */
    @ResponseBody
    @ApiResponse(code=200,message = "限行信息",response = ItcsCityLimitInfo.class)
    @RequestMapping(value = "/getXianxing", method = RequestMethod.POST)
    public Object getXianxing(
            HttpServletRequest request,
            @RequestParam(value = "city",defaultValue = "") String city){

        //验证手机号的正确性
        if(StringUtils.isEmpty( city )){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        ItcsCityLimitInfo xianxing = itcsCityLimitInfoService.getXianxingCity(city);

        if(null != xianxing){
            return  new ApiResult( ApiResultConstant.SUCCESS,xianxing);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }

}